package com.cg.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user")
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取当前会话对象，session的用法，可以用来储存信息
        HttpSession session = req.getSession();
        Object username=session.getAttribute("username");
        Object loginTime = session.getAttribute("longTime");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(username!=null){
            resp.getWriter().println("您好，"+username.toString());
            resp.getWriter().println("登陆时间"+loginTime.toString());
        }else{
            resp.getWriter().println("您尚未登陆！");
        }

//        Cookie[] cookies = req.getCookies();
//        PrintWriter writer = resp.getWriter();
//        if (cookies!=null){
//            for (Cookie cookie:cookies){
//                writer.println(cookie.getName()+":"+cookie.getValue());
//            }
//        }
    }
}
